import java.net.*;
import java.text.*;
import java.util.*;

public class Utils {
	//private static SimpleDateFormat _dateFormatter = new SimpleDateFormat("dd/MM/yy;HH:mm:ss");
	private static Random _random = new Random(System.currentTimeMillis());
	
	//Prefixes of the different messages
	private static final String UPDATE_MESSAGE = "update";
	private static final String REQUEST_ENTRY_MESSSAGE = "requestentry";
	private static final String REQUEST_PICTURE_MESSAGE = "requestpicture";
	private static final String ENTRY_MESSAGE = "entry";
	private static final String PICTURE_MESSAGE = "picture";
	
	public static String InetToString(InetAddress ip) {
		return String.format("%d.%d.%d.%d",
				(ip.getAddress()[0] + 256) % 256,
				(ip.getAddress()[1] + 256) % 256,
				(ip.getAddress()[2] + 256) % 256,
				(ip.getAddress()[3] + 256) % 256);
	}
	
	public static String[] parseMessage(String msg) {
		String[] parsed = msg.split("\\$|\\(|\\)|=|,|>");
		
		if (parsed[0].toLowerCase().startsWith("update"))
				return parseUpdate(parsed);
		if (parsed[0].toLowerCase().startsWith("entry"))
			return parseEntry(parsed);
		if (parsed[0].toLowerCase().startsWith("requestentry"))
			return parseRequestEntry(parsed);
		if (parsed[0].toLowerCase().startsWith("picture"))
			return parsePicture(parsed);
		if (parsed[0].toLowerCase().startsWith("requestpicture"))
			return parseRequestPicture(parsed);
		
		return null;
	}
	
	private static String[] parseUpdate(String[] msg) {
		List<String> result = new LinkedList<String>();
		
		result.add(msg[2]);
		result.add(msg[5]);
		
		return result.toArray(new String[0]);
	}
	
	private static String[] parseRequestEntry(String[] msg) {
		List<String> result = new LinkedList<String>();
		result.add(msg[2]);
		
		if (msg.length == 2)
			return result.toArray(new String[0]);
		
		for (int i = 4; i < msg.length; ++i)
			result.add(msg[i]);
		
		return result.toArray(new String[0]);
	}

	private static String[] parsePicture(String[] msg) {
		List<String> result = new LinkedList<String>();
		result.add(msg[3]);
		result.add(msg[6]);
		result.add(msg[9]);
		result.add(msg[12]);
		result.add(msg[15]);
		
		return result.toArray(new String[0]);
	}
	
	private static String[] parseEntry(String[] msg) {
		List<String> result = new LinkedList<String>();
		result.add(msg[3]);
		result.add(msg[6]);
		result.add(msg[9]);
		result.add(msg[12]);
		result.add(msg[15]);
		result.add(msg[18]);
		result.add(msg[21]);
		result.add(msg[24]);
		result.add(msg[27]);
		result.add(msg[30]);
		result.add(msg[33]);
		result.add(msg[36]);
		result.add(msg[39]);
		result.add(msg[42]);
		
		return result.toArray(new String[0]);
	}
	
	private static String[] parseRequestPicture(String[] msg) {
		List<String> result = new LinkedList<String>();
		
		result.add(msg[2]);
		result.add(msg[5]);
		
		if (msg.length > 6)
			result.add(msg[8]);
		
		if (msg.length > 9) {
			result.add(msg[10]);
			
			for (int i = 11; i < msg.length; ++i)
				result.add(msg[i]);
		}
		
		return result.toArray(new String[0]);
	}
	
	public static Date StringToDate(String str) {
		SimpleDateFormat _dateFormatter = new SimpleDateFormat("dd/MM/yy;HH:mm:ss");
		Date result = null;
		
		if (str.equals("0"))
			result = zeroDate();
		else { 
			try {result = _dateFormatter.parse(str);}
			catch (ParseException ex) {}
		}
		
		return result;
	}

	public final static String DateToString(Date date) {
		SimpleDateFormat _dateFormatter = new SimpleDateFormat("dd/MM/yy;HH:mm:ss");
		if (date.equals(zeroDate()))
			return "0";
		return _dateFormatter.format(date);
	}

	public final static Date now() {
		long now = System.currentTimeMillis();
		Date d = new Date(now);
		
		return StringToDate(DateToString(d));
	}
	
	public final static Date zeroDate() {
		//Calendar cal = Calendar.getInstance();
		//cal.set(0, 0, 0, 0, 0, 0);
		
		//return (Calendar)cal.clone();
		return new Date(0);
	}
	public final static int getNumberOfFragments(int picSize, int mss) { return (int)Math.ceil((picSize*1.0) / (mss / 2)); }
	
	public final static int getRandomInteger(int maxVal){ return _random.nextInt(maxVal); }
	
	public static String GetPrefixMessage(String message) {
		String result = "unknown";
		
		if (message.startsWith(UPDATE_MESSAGE))
			result = UPDATE_MESSAGE;
		else if (message.startsWith(REQUEST_ENTRY_MESSSAGE))
			result = REQUEST_ENTRY_MESSSAGE;
		else if (message.startsWith(REQUEST_PICTURE_MESSAGE))
			result = REQUEST_PICTURE_MESSAGE;
		else if (message.startsWith(ENTRY_MESSAGE))
			result = ENTRY_MESSAGE;
		else if (message.startsWith(PICTURE_MESSAGE))
			result = PICTURE_MESSAGE;

		return result;
	}
}
